package org.example.springblog.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.springblog.constants.Constants;
import org.example.springblog.model.BlogInfo;
import org.example.springblog.model.Result;
import org.example.springblog.model.UserInfo;
import org.example.springblog.service.BlogService;
import org.example.springblog.service.UserService;
import org.example.springblog.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/3
 * Time: 20:04
 */
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    public List<BlogInfo> getList(){
        log.info("获取博客列表");
        return blogService.getList();
    }

    @RequestMapping(value = "/getBlogDetail",method = RequestMethod.GET)
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("getBlogDetail,blogId:{}",blogId);
        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
        //判断作者是否为登录用户
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取用户id
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId != null && userId.equals(blogInfo.getUserId())){
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }

    @RequestMapping("/publish")
    public Result<String> publish(@RequestBody BlogInfo blogInfo, HttpServletRequest request){
        if (!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("标题或内容为空");
        }
        log.info("发布博客");
        try {
            //获取token
            String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
            //从token中获取用户id
            Integer userId = JwtUtils.getIdByToken(token);
            if (userId == null || userId <= 0){
                return Result.fail("用户不存在");
            }
            blogInfo.setUserId(userId);
            Integer ret = blogService.publish(blogInfo);
            //根据用户id
            Integer addRet = userService.addBlogCountByUserId(userId);
            if(ret == 1 && addRet == 1){
                return Result.success("success");
            }
        }catch (Exception e){
            log.error("发布失败,e:",e);
        }
        return Result.fail("内部错误,请联系管理员");
    }

    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客,blogInfo:{}",blogInfo);
        if(blogInfo.getId() == null
                || !StringUtils.hasLength(blogInfo.getTitle())
                ||  !StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        Integer result = blogService.update(blogInfo);
        return result == 1;
    }

    @RequestMapping("/delete")
    public Result<String> delete(Integer blogId, HttpServletRequest request){
        log.info("删除博客,blogId:{}",blogId);
        if (blogId <= 0){
            return Result.fail("博客不存在");
        }
        Integer result = blogService.delete(blogId);
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取用户id
        Integer userId = JwtUtils.getIdByToken(token);
        //根据用户id
        Integer subRet = userService.subBlogCountByUserId(userId);
        if (result == 1  && subRet == 1) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }
}
